/**   
 * Model Created on 2022年5月30日
 *   
 * Copyright (c) 2018 by Kunrong.HOo.
 */ 
package com.seaside.park.service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seaside.common.bean.Result;
import com.seaside.parkCommon.bean.ParkZone;
import com.seaside.parkCommon.bean.ReserveOrder;
import com.seaside.parkCommon.mapper.ReserveOrderMapper;

/**
 * 预约订单服务
 * @author <a href="mailto:hoocoral@hotmail.com">Kunrong.HOo</a>
 * @version 1.0
 */
@Service
public class ReserveOrderService {

	@Autowired
	private ReserveOrderMapper orderMapper;
	
	@Autowired
	private ParkService parkService;


	public Result<List<String>> getReserveMobiles(String carPlate) {
	
		QueryWrapper<ReserveOrder> wrapper = new QueryWrapper<>();
		wrapper.eq("car_plate", carPlate);
		List<ReserveOrder> orderList = orderMapper.selectList(wrapper);
		if (!CollectionUtils.isEmpty(orderList)) {
			List<String> mobileList = orderList.stream().map(ReserveOrder::getPhoneNumber).distinct().collect(Collectors.toList());
			return Result.OK("查询成功！", mobileList);
		}
		
		return Result.Error("未找到该车辆的预约订单！");
	}
	
	public Result<Boolean> setOrderFinished(String carPlate) {
		QueryWrapper<ReserveOrder> wrapper = new QueryWrapper<>();
		wrapper.eq("car_plate", carPlate);
		wrapper.eq("status", 0);
		List<ReserveOrder> orderList = orderMapper.selectList(wrapper);
		if (!CollectionUtils.isEmpty(orderList)) {
			orderList.forEach(order -> {
				if (order != null) {
					order.setStatus(1);
					orderMapper.updateById(order);
				}
			});
		}
		return Result.OK("查询成功！");
	}
	
	/**
	 * 查询当前用户订单列表
	 * @param page
	 * @param phoneNumber
	 * @return
	 */
	public Result<IPage<ReserveOrder>> getOrderPage(Page<ReserveOrder> page, String phoneNumber, List<Integer> parkPlaceIdList) {
		
		// 获取所有停车场
		final Map<String, ParkZone> parkZonesMap = parkService.getAllParkZonesMap();
		
		LambdaQueryWrapper<ReserveOrder> wrapper = new QueryWrapper<ReserveOrder>().lambda()
				.eq(StringUtils.isNotBlank(phoneNumber), ReserveOrder::getPhoneNumber, phoneNumber)
				.in(parkPlaceIdList != null, ReserveOrder::getParkPlaceId, parkPlaceIdList);
		wrapper.orderByDesc(ReserveOrder::getOrderTime);
		
		IPage<ReserveOrder> iPage = orderMapper.selectPage(page, wrapper);
		List<ReserveOrder> orderList = iPage.getRecords();
		if (!CollectionUtils.isEmpty(orderList)) {
			orderList.forEach(order -> {
				if (order != null) {
					ParkZone parkZone = parkZonesMap.get(order.getParkZoneId());
					if (parkZone != null) {
						order.setParkZoneName(parkZone.getZoneName());
						order.setPhoneNumber("***");
					}
				}
			});
		}
        return Result.OK("查询成功！", iPage);
	}
}
